package com.alinesno.cloud.gateway.admin.proxy;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;

import com.alinesno.cloud.common.facade.enums.HasStatusEnums;
import com.alinesno.cloud.gateway.admin.entity.GateBusinessServerEntity;
import com.alinesno.cloud.gateway.admin.service.IGateBusinessServerService;
import com.alinesno.cloud.gateway.core.dispather.mina.MinaSocketProxyFactory;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.BusinessServerBean;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.MessageBean;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.MessageBuilder;

import cn.hutool.core.date.DateTime;

/**
 * Socket路由基础代理类
 * 
 * @author WeiXiaoJin
 * @since 2020年1月31日 下午19:45:08
 */
public abstract class BaseProxy implements MinaSocketProxyFactory {

	/**
	 * 网关服务
	 */
	@Autowired
	protected IGateBusinessServerService serverService;

	/**
	 * 校验是否已经触发接口规则
	 */
	@Override
	public MessageBean handle(BusinessServerBean server, IoSession session, MessageBean message) {
		GateBusinessServerEntity e = serverService.findById(server.getServerId()).get();
		e.setHasRequestCount(e.getHasRequestCount() == null ? 1 : e.getHasRequestCount() + 1);
		
		// 判断IP
		String clientIP = (String) session.getAttribute("KEY_SESSION_CLIENT_IP");

		MessageBean ruleMessage = filterSession(session, e);
		if (ruleMessage != null) {
			e.setHasErrorCount(e.getHasErrorCount() == null ? 1 : e.getHasErrorCount() + 1);
			serverService.save(e);
			return ruleMessage;
		}

		return proxy(server, session, message, e, clientIP);
	}

	/**
	 * 实现业务功能
	 * 
	 * @param server
	 * @param session
	 * @param message
	 * @param e
	 * @param clientIP
	 * @return
	 */
	protected abstract MessageBean proxy(BusinessServerBean server, IoSession session, MessageBean message,
			GateBusinessServerEntity e, String clientIP);

	/**
	 * 过滤会话
	 * 
	 * @param session
	 * @param e
	 * @return
	 */
	private MessageBean filterSession(IoSession session, GateBusinessServerEntity e) {

		if (e.getHasStatus() != HasStatusEnums.LEGAL.value) { // 是否打开接口
			return MessageBuilder.close();
		}

		// >>>>>>>>>>>>>>>> 判断是否达到运行时间_start >>>>>>>>>>>>>>>>>>>
		long now = DateTime.now().getTime();
		Date endTime = e.getOpenTime();
		if (endTime != null) {
			if (DateTime.of(endTime).getTime() - now < 0) {
				return MessageBuilder.nowOpenTime();
			}
		}

		Date startTime = e.getCloseTime();
		if (startTime != null) {
			if (DateTime.of(startTime).getTime() - now > 0) {
				return MessageBuilder.nowOpenTime();
			}
		}
		// >>>>>>>>>>>>>>>> 判断是否达到运行时间_end >>>>>>>>>>>>>>>>>>>

		// 判断IP
		String clientIP = (String) session.getAttribute("KEY_SESSION_CLIENT_IP");
		String accessIp = e.getAccessIp(); // 允许的IP(以,作为分隔符)
		if (StringUtils.isNoneBlank(accessIp)) {
			if (accessIp.contains(clientIP)) {
				return null;
			}
		}

		String denyIp = e.getDenyIp(); // 限制IP(以,作为分隔符)
		if (StringUtils.isNoneBlank(denyIp)) {
			if (denyIp.contains(clientIP)) {
				return MessageBuilder.deny();
			}
		}

//		int limitRequest = e.getLimitRequest(); // 限流(-1表示不限流)
//		String authType = e.getAuthType(); // 认证方式
//		int retrics = e.getRetrics(); // 重试次数(默认为1次)

		return null;
	}
}
